#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义学生信息结构体(姓名，性别，专业，生日，家庭住址，专业成绩)
typedef struct Student {
	char name[50];
	char gender[10];
	char major[50];
	int birthYear;
	char address[100];
	float englishScore;
} Student;

// 定义链表节点结构体
typedef struct Node {
	Student data;
	struct Node* next;
} Node;

// 链表类结构体
typedef struct {
	Node* head;
	int count;
} StudentList;

// 函数声明
void addStudent(StudentList* list, Student student);          //新增学生信息
void deleteStudent(StudentList* list, const char* name);   //删除学生信息
void importStudents(StudentList* list, const char* filename); //导入学生信息
Student* searchStudent(StudentList* list, const char* name);  //学生信息搜索
void countStudents(StudentList* list, const char* criteria); //学生信息统计
void sortStudentsByEnglishScore(StudentList* list);          //按专业成绩排序
void saveStudents(StudentList* list, const char* filename);   //学生信息保存
void printStudent(const Student* student);                    //打印学生信息
void printMenu();                                            //打印菜单
StudentList* createStudentList();                            //创建学生链表
void freeStudentList(StudentList* list);                    //释放学生链表
void displayAllStudents(StudentList* list);                 //显示所有学生信息
void modifySimpleInfo(StudentList* list, const char* name);  //修改学生信息

int main() {
	StudentList* list = createStudentList();
	int choice;
	do {
		printMenu();
		scanf("%d", &choice);
		switch (choice) {
		case 1:
			{
				Student student;
				printf("输入姓名: ");
				scanf("%s", student.name);
				while (getchar()!= '\n');
				printf("输入性别: ");
				scanf("%s", student.gender);
				while (getchar()!= '\n');
				printf("输入专业: ");
				scanf("%s", student.major);
				while (getchar()!= '\n');
				printf("输入出生日期: ");
				scanf("%d", &student.birthYear);
				while (getchar()!= '\n');
				printf("输入住址: ");
				scanf("%s", student.address);
				while (getchar()!= '\n');
				printf("输入专业成绩: ");
				scanf("%f", &student.englishScore);
				addStudent(list, student);
				break;
			}
		case 2:
			{
				char name[50];
				printf("输入姓名: ");
				
				scanf("%s", name);
				deleteStudent(list, name);
				break;
			}
		case 3:
			{
				char name[50];
				printf("输入姓名: ");
				scanf("%s", name);
				Student* student = searchStudent(list, name);
				if (student) {
					printStudent(student);
				}
				else {
					printf("未查询到该学生.\n");
				}
				break;
			}
		case 4:
			sortStudentsByEnglishScore(list);
			break;
		case 5:
			char name[50];
			printf("输入要修改信息的学生姓名: ");
			scanf("%s", name);
			modifySimpleInfo(list, name);
			break;
		case 6:
			printf("正在加载，即将显示所有学生信息\n");
			displayAllStudents(list);
			break;
		case 7:
			{
				printf("正在退出。。。\n");
				break;
			}
		default:
			printf("请输入正确地选项.\n");
		}
	} while (choice!= 7);
	
	freeStudentList(list);
	return 0;
}

// 函数定义
//创建学生链表
StudentList* createStudentList() {
	StudentList* list = (StudentList*)malloc(sizeof(StudentList));
	list->head = NULL;
	list->count = 0;
	return list;
}
//释放学生链表
void freeStudentList(StudentList* list) {
	Node* current = list->head;
	while (current!= NULL) {
		Node* next = current->next;
		free(current);
		current = next;
	}
	free(list);
}
//新增学生信息
void addStudent(StudentList* list, Student student) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = student;
	newNode->next = list->head;
	list->head = newNode;
	list->count++;
}
//删除学生信息
void deleteStudent(StudentList* list, const char* name) {
	Node** current = &(list->head);
	while (*current!= NULL) {
		if (strcmp((*current)->data.name, name) == 0) {
			Node* temp = *current;
			*current = (*current)->next;
			free(temp);
			list->count--;
			return;
		}
		current = &((*current)->next);
	}
}
//搜索学生信息
Student* searchStudent(StudentList* list, const char* name) {
	Node* current = list->head;
	while (current!= NULL) {
		if (strcmp(current->data.name, name) == 0) {
			return &(current->data);
		}
		current = current->next;
	}
	return NULL;
}
// 按专业成绩降序排序
void sortStudentsByEnglishScore(StudentList* list) {
	Node* current;
	Node* index;
	Student temp;
	if (list->head == NULL) {
		return;
	} else {
		for (current = list->head; current->next!= NULL; current = current->next) {
			for (index = current->next; index!= NULL; index = index->next) {
				if (current->data.englishScore < index->data.englishScore) {
					temp = current->data;
					current->data = index->data;
					index->data = temp;
				}
			}
		}
	}
	
// 打印排序后的姓名和成绩
	current = list->head;
	while (current!= NULL) {
		printf("姓名：%s，专业成绩：%.2f\n", current->data.name, current->data.englishScore);
		current = current->next;
	}
}
//打印学生信息
void printStudent(const Student* student) {
	printf("姓名: %s\n", student->name);
	printf("性别: %s\n", student->gender);
	printf("专业: %s\n", student->major);
	printf("出生日期: %d\n", student->birthYear);
	printf("家庭住址: %s\n", student->address);
	printf("英语成绩: %.2f\n", student->englishScore);
}
//打印菜单
void printMenu() {
	printf("\n1. 新增学生信息\n");
	printf("2. 删除学生信息\n");
	printf("3. 搜索学生信息（按姓名）\n");
	printf("4. 按专业成绩排序--降序\n");
	printf("5. 修改学生信息\n");
	printf("6. 显示所有学生信息\n");
	printf("7.  退出\n");
	printf("请选择选项: ");
}
//显示所有学生信息
void displayAllStudents(StudentList* list) {
	Node* current = list->head;
	if (current == NULL) {
		printf("暂无学生信息记录。\n");
		return;
	}
	
	while (current!= NULL) {
		printStudent(&current->data);
		current = current->next;
	}
}
//修改学生信息
void modifySimpleInfo(StudentList* list, const char* name) {
	Student* studentToModify = searchStudent(list, name);
	if (studentToModify) {
		int choice;
		do {
			printf("\n可修改的信息选项：\n");
			printf("1. 姓名\n");
			printf("2. 性别\n");
			printf("3. 专业\n");
			printf("4. 完成修改并退出\n");
			printf("请选择要修改的选项: ");
			
			// 处理输入选项时可能出现的非数字输入情况
			if (scanf("%d", &choice)!= 1) {
				printf("输入错误，请输入正确的数字选项。\n");
				// 清除输入缓冲区中的无效输入
				while (getchar()!= '\n');
				continue;
			}
			
			switch (choice) {
			case 1:
				{
					char newName[50];
					printf("输入新的姓名: ");
					scanf("%s", newName);
					strcpy(studentToModify->name, newName);
					break;
				}
			case 2:
				{
					char newGender[10];
					printf("输入新的性别: ");
					scanf("%s", newGender);
					strcpy(studentToModify->gender, newGender);
					break;
				}
			case 3:
				{
					char newMajor[50];
					printf("输入新的专业: ");
					scanf("%s", newMajor);
					strcpy(studentToModify->major, newMajor);
					break;
				}
			case 4:
				break;
			default:
				printf("请输入正确的选项。\n");
			}
		} while (choice!= 4);
		
		// 修改完成后不退出程序，回到主循环继续等待用户选择
		printf("学生信息已成功修改。\n");
	} else {
		printf("未找到要修改信息的学生。\n");
	}
}
